import React from 'react';

import {Radio} from 'antd';
const RadioGroup = Radio.Group;

import styles from './index.less';

export default class RadioEx extends React.Component{

  state = {
    value:null
  }

  onValueChange(e){
    var v = e.target.value;
    console.log("Here:", v);

    var validate = this.props.validate;
    this.props.emitter.emit('onValueChange', this.props.name, v);
    this.setState({
      value:v
    });
  }

  iemi = null;

  reset(){
    this.setState({
      value:this.props.defaultValue
    })
  }

  componentWillReceiveProps(n){
  }

  componentWillMount(){
    this.reset();

    this.iemi = this.props.emitter.addListener('onReset', ()=>{this.reset()});
    this.props.emitter.emit('onValueChange', this.props.name, this.props.defaultValue);
  }

  componentWillUnmount(){
    this.iemi.remove();
  }

  render(){
    return <RadioGroup
      value = {this.state.value}
      defaultValue = {this.props.defaultValue}
      onChange = {(e) => {
        this.onValueChange(e);
      }}
    >
      {this.props.options.map((v, idx)=>{
        return <Radio key = {idx} value = {v.value}>{v.text}</Radio>
      })}
    </RadioGroup>
  }
}